package f.h.dragon.bjsuperpass;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import f.h.dragon.otc15.OTC15Tester;

import junit.framework.TestCase;

/**
 * HttpClient tester
 * 
 * @author F.H Dragon(wenlong.meng@gmail.com)
 * @version 1.0 at 2011/10/02
 * @since 1.0
 */
public class HttpClientTester extends TestCase {

	// local variables
	/**
	 * logger
	 */
	Logger logger = Logger.getLogger(getClass());

	/**
	 * Sets up the fixture, for example, open a network connection. This method
	 * is called before a test is executed.
	 */
	@Override
	protected void setUp() throws Exception {
		logger.debug("setUp");
		super.setUp();
	}

	/**
	 * httpClient test
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public void testHttpClient() throws ClientProtocolException, IOException {
		logger.debug("testHttpClient...");
		long base_card_id = 100000000000001000L;
		HttpClient httpclient = new DefaultHttpClient();
		OutputStream os = null;
		InputStream is = null;
		for (int i = 0; i < 1000; i++) {
			base_card_id += 1;
			logger.debug("Begin: base_card_id=" + base_card_id + "...");
			os = new FileOutputStream(new File("/datas/fhdragon/bjsuperpass/" + base_card_id + ".fhdragon"));
			HttpGet httpget = new HttpGet(
					"http://www.bjsuperpass.com/pagecontrol.do?object=ecard&action=query&card_id="
							+ base_card_id);
			
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				is = entity.getContent();
				byte[] tmp = new byte[2048];
				int length;
				while ((length = is.read(tmp)) != -1) {
					os.write(tmp, 0, length);
				}
				is.close();
				os.flush();
				os.close();
				is = null;
				os = null;
			}
			logger.debug("End: base_card_id=" + base_card_id + "!");
		}
	}

	/**
	 * Tears down the fixture, for example, close a network connection. This
	 * method is called after a test is executed.
	 */
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		logger.debug("tearDown");
	}

	public class Task implements Runnable {

		// local variables
		/**
		 * card Id
		 */
		long cardId = 0;

		// constructor
		/**
		 * Constructor
		 * 
		 * @param cardId
		 */
		public Task(long cardId) {
			this.cardId = cardId;
		}

		/**
		 * main run
		 */
		@Override
		public void run() {
			
			OutputStream os = null;
			InputStream is = null;
			try{
				HttpClient httpclient = new DefaultHttpClient();
				os = new FileOutputStream(new File("/datas/fhdragon/bjsuperpass/" + cardId + ".fhdragon"));
				HttpGet httpget = new HttpGet(
						"http://www.bjsuperpass.com/pagecontrol.do?object=ecard&action=query&card_id="
								+ cardId);
				
				HttpResponse response = httpclient.execute(httpget);
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					is = entity.getContent();
					byte[] tmp = new byte[2048];
					int length;
					while ((length = is.read(tmp)) != -1) {
						os.write(tmp, 0, length);
					}
					os.flush();
				}
			}catch(Exception e){
				logger.error("cardId:" + cardId + ", error:" + e);
			}finally{
				if(os != null){
					try {
						os.close();
					} catch (IOException e) {
						logger.error("cardId:" + cardId + ", error:" + e);
					}
				}
				if(is != null){
					try {
						is.close();
					} catch (IOException e) {
						logger.error("cardId:" + cardId + ", error:" + e);
					}
				}
			}
			
		}
	}
	public static void main(String[] args) throws ClientProtocolException, IOException{
		int base_card_id = 1060095000;
		new OTC15Tester().testBJSuperPass(base_card_id);
	}
	
	/**
	 * httpClient test
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public void testBJSuperPass(int base_card_id) throws ClientProtocolException, IOException {
		HttpClient httpclient = new DefaultHttpClient();
//		OutputStream os = null;
		InputStream is = null;
		for (int i = 0; i < 5000; i++) {
			base_card_id += 1;
			logger.debug("Begin: base_card_id=" + base_card_id + "...");
//			os = new FileOutputStream(new File("/datas/fhdragon/bjsuperpass/100075" + base_card_id + ".fhdragon"));
			HttpGet httpget = new HttpGet(
					"http://www.bjsuperpass.com/pagecontrol.do?object=ecard&action=query&card_id=100075"
							+ base_card_id + "0");
			
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				is = entity.getContent();
				byte[] tmp = new byte[2048];
				int length;
				StringBuilder sb = new StringBuilder();
				while ((length = is.read(tmp)) != -1) {
					System.out.println(new java.sql.Timestamp(new java.util.Date().getTime()).toString() + ":" + base_card_id);
//					os.write(tmp, 0, length);
				}
//				is.close();
//				os.flush();
//				os.close();
//				is = null;
//				os = null;
			}
			logger.debug("End: base_card_id=" + base_card_id + "!");
		}
	}
	
	public void writeToFile(OutputStream os){
		
	}
	
	private Executor executor = Executors.newFixedThreadPool(3);

}
